package com.sxjh.mapper;

import com.sxjh.entity.Settlement;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sxjh.entity.bo.Finance7ListBo;
import com.sxjh.entity.bo.Finance7SettleBo;
import com.sxjh.entity.param.Finance7List3Param;
import com.sxjh.entity.param.Finance7SettleParam;
import com.sxjh.entity.result.Finance7List3Result;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 结算列表 Mapper 接口
 * </p>
 *
 * @author zixin
 * @since 2021-08-03
 */
public interface SettlementMapper extends BaseMapper<Settlement> {


  // 查询最早的发货时间
//  @Select("select min(created_at) from trucking_order_details where id in (${ids})")
  @Select({
          "<script> " ,
            "select min(created_at) from trucking_order_details where id in " ,
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'> #{id} </foreach> " ,
          "</script>"
  })
  LocalDate getStartDate(@Param("ids") List<Integer> ids);

  // 查询净重
//  @Select("select sum(go2.weighbridge_weight) as weighbridge_weight from trucking_order_details tod left join generate_orders go2 on go2.trucking_order_detail_id = tod.id where tod.id in (${ids})")
  @Select({
          "<script> " ,
            "select sum(go2.weighbridge_weight) as weighbridge_weight from trucking_order_details tod left join generate_orders go2 on go2.trucking_order_detail_id = tod.id where tod.id in " ,
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'> #{id} </foreach> " ,
          "</script>"
  })
  BigDecimal getWeighbridgeWeight(@Param("ids") List<Integer> ids);

  // 更新结算单
//  @Update("update trucking_order_details tod\n" +
//          "left join trucking_orders to2 on to2.id = tod.trucking_order_id\n" +
//          "left join planned_orders po on po.id = to2.planned_order_id\n" +
//          "set tod.backfill_id = #{id}\n" +
//          "where tod.backfill_id = 0 " +
//          "and po.contract_no = #{contractNo}\n" +
//          "and tod.id in (${ids})")
  @Update({
          "<script> " ,
            "update trucking_order_details tod " +
            "left join trucking_orders to2 on to2.id = tod.trucking_order_id " +
            "left join planned_orders po on po.id = to2.planned_order_id " +
            "set tod.backfill_id = #{id} " +
            "where tod.backfill_id = 0 " +
            "and po.contract_no = #{contractNo} " +
            "and tod.id in " ,
            "<foreach item='todId' collection='ids' open='(' separator=',' close=')'> #{todId} </foreach> " ,
          "</script>"
  })
  Integer setBackfillId(@Param("id") Integer id, @Param("contractNo") String contractNo, @Param("ids") List<Integer> ids);

  // 焦炭结算列表
  List<Finance7List3Result> list(Finance7List3Param param);

  // 生成结算单的查询数据
  List<Finance7SettleBo> settle(Finance7SettleParam param);

}